import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWechatConfigService extends BaseService<any> {
  private readonly logger = new Logger(CoreWechatConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getWechatConfig
   * 对应 PHP: CoreWechatConfigService_core::getWechatConfig()
   * 逻辑类型: undefined - undefined
   */
  async getWechatConfig(site_id: number) {
    // 基于PHP真实逻辑: getWechatConfig
    // PHP原文: $info = ( new CoreConfigService() )->getConfig($site_id, ConfigKeyDict::WECHAT)[ 'value' ] ?? [];         return [             'wechat_name' => $info[...
const info = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.WECHAT)[ 'value' ] || [];
        return [
            wechat_name: info[ 'wechat_name' ] || '',//公众号名称
            wechat_original: info[ 'wechat_original' ] || '',//原始ID
            app_id: info[ 'app_id' ] || '',//AppID
            app_secret: info[ 'app_secret' ] || '',//AppSecret
            qr_code: info[ 'qr_code' ] || '',//公众号二维码
            token: info[ 'token' ] || '',
            encoding_aes_key: info[ 'encoding_aes_key' ] || '',
            encryption_type: info[ 'encryption_type' ] || 'not_encrypt',//加解密模式   not_encrypt 明文   compatible 兼容  safe 安全
            is_authorization: info[ 'is_authorization' ] || 0
        ];
    }

    /**
     * 设置微信公众号配置
     * @param int site_id
     * @param data
     * @return SysConfig|bool|Model
     */
    async setWechatConfig(int site_id, data)
    {
        const old = this.getWechatConfig(site_id];
        const config = [
            wechat_name: data[ 'wechat_name' ] || '',//公众号名称
            wechat_original: data[ 'wechat_original' ] || '',//原始ID
            app_id: data[ 'app_id' ] || '',//AppID
            app_secret: data[ 'app_secret' ] || '',//AppSecret
            qr_code: data[ 'qr_code' ] || '',//公众号二维码
            token: data[ 'token' ] || '',
            encoding_aes_key: data[ 'encoding_aes_key' ] || '',
            encryption_type: data[ 'encryption_type' ] || '',
            is_authorization: data[ 'is_authorization' ] || old[ 'is_authorization' ]
        );
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT, config);
    }

    /**
     * 查询微信需要的静态信息
     * @return */
    async getWechatStaticInfo(site_id)
    {
        const wap_domain = ( this.coreSysConfigService ).getSceneDomain(site_id)[ 'wap_domain' ] || '';
        const wap_domain = str_replace('http://', '', wap_domain];
        const wap_domain = str_replace('https://', '', wap_domain);
        return [
            serve_url: (string) url('/api/wechat/serve/' . site_id, [), '', true),
            business_domain: wap_domain,
            js_secure_domain: wap_domain,
            web_auth_domain: wap_domain,
            encryption_type: WechatDict.getEncryptionType()
        ];
    }

    /**
     * 获取小程序授权信息
     * @param int site_id
     * @return mixed
     */
    async getWechatAuthorizationInfo(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO);
    }

    /**
     * 设置小程序授权信息
     * @param int site_id
     * @param config
     * @return SysConfig|bool|Model
     */
    async setWechatAuthorizationInfo(int site_id, config)
    {
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO, config);
    }
}
  }

  /**
   * setWechatConfig
   * 对应 PHP: CoreWechatConfigService_core::setWechatConfig()
   * 逻辑类型: undefined - undefined
   */
  async setWechatConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setWechatConfig
    // PHP原文: $old = $this->getWechatConfig($site_id);         $config = [             'wechat_name' => $data[ 'wechat_name' ] ?? '',//公众号名称             'wechat_ori...
const old = this.getWechatConfig(site_id];
        const config = [
            wechat_name: data[ 'wechat_name' ] || '',//公众号名称
            wechat_original: data[ 'wechat_original' ] || '',//原始ID
            app_id: data[ 'app_id' ] || '',//AppID
            app_secret: data[ 'app_secret' ] || '',//AppSecret
            qr_code: data[ 'qr_code' ] || '',//公众号二维码
            token: data[ 'token' ] || '',
            encoding_aes_key: data[ 'encoding_aes_key' ] || '',
            encryption_type: data[ 'encryption_type' ] || '',
            is_authorization: data[ 'is_authorization' ] || old[ 'is_authorization' ]
        );
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT, config);
    }

    /**
     * 查询微信需要的静态信息
     * @return */
    async getWechatStaticInfo(site_id)
    {
        const wap_domain = ( this.coreSysConfigService ).getSceneDomain(site_id)[ 'wap_domain' ] || '';
        const wap_domain = str_replace('http://', '', wap_domain];
        const wap_domain = str_replace('https://', '', wap_domain);
        return [
            serve_url: (string) url('/api/wechat/serve/' . site_id, [), '', true),
            business_domain: wap_domain,
            js_secure_domain: wap_domain,
            web_auth_domain: wap_domain,
            encryption_type: WechatDict.getEncryptionType()
        ];
    }

    /**
     * 获取小程序授权信息
     * @param int site_id
     * @return mixed
     */
    async getWechatAuthorizationInfo(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO);
    }

    /**
     * 设置小程序授权信息
     * @param int site_id
     * @param config
     * @return SysConfig|bool|Model
     */
    async setWechatAuthorizationInfo(int site_id, config)
    {
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO, config);
    }
}
  }

  /**
   * getWechatStaticInfo
   * 对应 PHP: CoreWechatConfigService_core::getWechatStaticInfo()
   * 逻辑类型: undefined - undefined
   */
  async getWechatStaticInfo(site_id: any) {
    // 基于PHP真实逻辑: getWechatStaticInfo
    // PHP原文: $wap_domain = ( new CoreSysConfigService() )->getSceneDomain($site_id)[ 'wap_domain' ] ?? '';         $wap_domain = str_replace('http://', '', $wap_do...
const wap_domain = ( this.coreSysConfigService ).getSceneDomain(site_id)[ 'wap_domain' ] || '';
        const wap_domain = str_replace('http://', '', wap_domain];
        const wap_domain = str_replace('https://', '', wap_domain);
        return [
            serve_url: (string) url('/api/wechat/serve/' . site_id, [), '', true),
            business_domain: wap_domain,
            js_secure_domain: wap_domain,
            web_auth_domain: wap_domain,
            encryption_type: WechatDict.getEncryptionType()
        ];
    }

    /**
     * 获取小程序授权信息
     * @param int site_id
     * @return mixed
     */
    async getWechatAuthorizationInfo(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO);
    }

    /**
     * 设置小程序授权信息
     * @param int site_id
     * @param config
     * @return SysConfig|bool|Model
     */
    async setWechatAuthorizationInfo(int site_id, config)
    {
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO, config);
    }
}
  }

  /**
   * getWechatAuthorizationInfo
   * 对应 PHP: CoreWechatConfigService_core::getWechatAuthorizationInfo()
   * 逻辑类型: undefined - undefined
   */
  async getWechatAuthorizationInfo(site_id: number) {
    // 基于PHP真实逻辑: getWechatAuthorizationInfo
    // PHP原文: return ( new CoreConfigService() )->getConfigValue($site_id, ConfigKeyDict::WECHAT_AUTHORIZATION_INFO);     }      /**      * 设置小程序授权信息      * @param ...
return ( this.coreConfigService ).getConfigValue(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO);
    }

    /**
     * 设置小程序授权信息
     * @param int site_id
     * @param config
     * @return SysConfig|bool|Model
     */
    async setWechatAuthorizationInfo(int site_id, config)
    {
        return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO, config);
    }
}
  }

  /**
   * setWechatAuthorizationInfo
   * 对应 PHP: CoreWechatConfigService_core::setWechatAuthorizationInfo()
   * 逻辑类型: undefined - undefined
   */
  async setWechatAuthorizationInfo(site_id: any[], config: any[]) {
    // 基于PHP真实逻辑: setWechatAuthorizationInfo
    // PHP原文: return ( new CoreConfigService() )->setConfig($site_id, ConfigKeyDict::WECHAT_AUTHORIZATION_INFO, $config);     } }...
return ( this.coreConfigService ).setConfig(site_id, ConfigKeyDict.WECHAT_AUTHORIZATION_INFO, config);
    }
}
  }
}
